package xGRUx.MCdirGlztab.Views;

import java.util.List;
import xGRUx.MCdirGlztab.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StoryViewAdapter extends ArrayAdapter<StoryItem> {

	private Context m_Context;
	private List<StoryItem> m_Items;
	private Typeface m_Font;
	
	public StoryViewAdapter(Context context, int textViewResourceId,
			List<StoryItem> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.m_Items = objects;
		this.m_Context = context;
		this.m_Font = Typeface.createFromAsset(m_Context.getAssets(), "fonts/DroidSans.ttf");
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
         if (view == null) {
             LayoutInflater vi = (LayoutInflater)this.m_Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             view = vi.inflate(R.layout.storyitemview, null);
         }
         
         StoryItem o = this.m_Items.get(position);
         if (o != null) {
                 TextView tt = (TextView) view.findViewById(R.id.lblTitle);
                 TextView time = (TextView) view.findViewById(R.id.lblTimeToGo);
                 TextView loc = (TextView) view.findViewById(R.id.lblEventLoc);
                 ImageView img = (ImageView) view.findViewById(R.id.imgMsgType);
                 
                 if (tt != null) {
                     tt.setText(subString(o.getTitle(), 47));
                     addFontFace(tt);
                 }
                 if (time != null) {
                	 time.setText(subString(o.getTimeToGo(), 120));
                	 addFontFace(time);
                 }
                 if (loc != null) {
                	 loc.setText(subString(o.getGoLocation(), 120));
                	 addFontFace(loc);
                 }
                 if (img != null) {
                	 img.setImageResource(o.getImgSrcId());                           
                 }
         }
         return view;
	}
	
	private String subString(String src, int lim) {
		if(src.length() > lim) {
			return src.substring(0, lim) + "..";
		}
		return src;
	}
	
	private void addFontFace(TextView v) {
		v.setTypeface(this.m_Font);
	}

}
